$(document).ready(function() {
    var slides = $('.carousel-slide');
    var indicatorsContainer = $('.carousel-indicators');
    var prevButton = $('.carousel-control.prev');
    var nextButton = $('.carousel-control.next');
    var currentSlide = 0;

    // 动态生成小圆点
    for (var i = 0; i < slides.length; i++) {
        var indicator = $('<span>').addClass('indicator');
        if (i === 0) {
            indicator.addClass('active');
        }
        indicatorsContainer.append(indicator);
    }

    var indicators = $('.carousel-indicators.indicator');

    function showSlide(n) {
        slides.removeClass('active');
        indicators.removeClass('active');
        currentSlide = (n + slides.length) % slides.length;
        slides.eq(currentSlide).addClass('active');
        indicators.eq(currentSlide).addClass('active');
    }

    function nextSlide() {
        showSlide(currentSlide + 1);
    }

    function prevSlide() {
        showSlide(currentSlide - 1);
    }

    prevButton.click(prevSlide);
    nextButton.click(nextSlide);

    // 重新获取小圆点元素后绑定点击事件
    indicators = $('.carousel-indicators .indicator');
    indicators.click(function() {
        showSlide($(this).index());
    });

    // 添加定时器，每5秒自动切换到下一张幻灯片
    var timer = setInterval(nextSlide, 2000);

    // 获取轮播图容器元素
    var carousel = $('#carousel');
    // 鼠标悬停时暂停定时器
    carousel.hover(function() {
        clearInterval(timer);
    }, function() {
        // 鼠标移开后重新启动定时器
        timer = setInterval(nextSlide, 2000);
    });

    showSlide(currentSlide);
});

